package cn.zptc.web;

import cn.zptc.dao.impl.FavoriteDaoImpl;
import cn.zptc.entity.Favorite;
import cn.zptc.service.FavoriteService;
import cn.zptc.service.impl.FavoriteServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/client/toEditMyFavoritePage")
public class ToEditMyFavoritePageServlet extends HttpServlet {

    private FavoriteService favoriteService;

    @Override
    public void init() throws ServletException {
        super.init();

        favoriteService = new FavoriteServiceImpl(new FavoriteDaoImpl());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            int favoriteId = Integer.parseInt(req.getParameter("favoriteId"));
            //todo:假定userid=1
            Favorite favorite=favoriteService.getFavorite(favoriteId,1);

            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.print("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "    <head>\n" +
                    "        <meta charset=\"UTF-8\">\n" +
                    "        <title>修改一个收藏</title>\n" +
                    "    </head>\n" +
                    "    <body>\n" +
                    "        <h3>修改一个收藏</h3>\n" +
                    "        <form action=\"\" method=\"post\">\n" +
                    "            <input id=\"id\" type=\"hidden\" value='"+favorite.getId()+"'>\n" +
                    "            <input id=\"category_id\" type=\"hidden\" value='"+favorite.getCategoryId()+"'>\n" +
                    "            <table width=\"70%\" >\n" +
                    "                <tr><td >收藏名称：<input id=\"favorite_name\" type=\"text\" name=\"favorite_name\" placeholder=\"1-100个字符\" value='"+favorite.getFavoriteName()+"'/></td></tr>\n" +
                    "                <tr><td >链接地址：<input id=\"link_url\" type=\"text\" name=\"link_url\" placeholder=\"1-255个字符\" value='"+favorite.getLinkUrl()+"'/></td></tr>\n" +
                    "                <tr><td id=\"my_link_category\">链接分类：</td></tr>\n" +
                    "                <tr><td >链接所在网站的登录名：<input id=\"login_name\" type=\"text\" name=\"login_name\" placeholder=\"1-100个字符\" value='"+favorite.getLoginName()+"'/></td></tr>\n" +
                    "                <tr><td >登录密码：<input id=\"password\" type=\"text\" name=\"password\" placeholder=\"1-50个字符\" value='"+favorite.getPassword()+"' /></td></tr>\n" +
                    "                <tr><td ><input id=\"btn\" type=\"button\" value=\"保存\"/></td></tr>\n" +
                    "            </table>\n" +
                    "        </form>\n" +
                    "\n" +
                    "        <script src=\"../js/axios-0.18.0.js\"></script>\n" +
                    "        <script src=\"my_favorite_edit.js\"></script>\n" +
                    "    </body>\n" +
                    "</html>");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
